<?php
/**
 *	@name TimeSheet
 *	@copyright : SMART - ULg (www.smart.ulg.ac.be)		
 *
**/


/**
 * Class TimeSheet : 
 * 
 * 
 * $Rev$ 
 * $LastChangedRevision$
 * $LastChangedBy$
 * $LastChangedDate$
 * $HeadURL$
 *
 * @name ProjectsListBroker
 * @package webservices/broker
 * @author : JPh HUMBLET
 * @copyright : SMART - ULg (www.smart.ulg.ac.be)
 * @category : ExAMS::Core
 *
**/

require_once("../classes/tracking/AbstractTimeSheetItem.php");

class TimeSheet extends AbstractTimeSheetItem
{
	
	/**
	 * appendEntry
	 * undocumented function
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function appendEntry( $te ) {
		$uids = $this->xpath("//Entry[last()]");
		$uid = (int) $uids[0]->UID;
		$te->UID = $uid+1;
		return $this->TimeEntries->appendSXE($te);
	}
	
	/**
	 * updateEntry
	 * undocumented function
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function updateEntry( $te ) {
		$uid = (int)$te->UID;
		$entries = $this->xpath("//Entry[UID=" .(int)$uid. "]");	
		if (count($entries) == 1) {
			$entries[0] = $te;
			return $this->save();
		}
		return false;
	}
	
	/**
	 * deleteEntry
	 * undocumented function
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function deleteEntry( $uid ) {
		$entries = $this->xpath("//Entry[UID=" .(int)$uid. "]");
		if (count($entries) == 1) {
			unset($entries[0]);
			return $this->save();
		}
		return false;
	}
	
	/**
	 * save
	 * 
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function save() {
		if (!isset($_SESSION["timesheet"])) { return false; }
		if (!$file = fopen($_SESSION['timesheet'], "w+" )) { return false; }
		if (!fwrite($file, $this->asXML())) { return false; }
		fclose($file);
		return true;
	}

	/**
	 * getByTaskUID
	 * undocumented function
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function getByTaskUID($tUID) {
		$xpath = "//Entry[TaskUID =". $tUID ."]";
		return $this->xpath($xpath);
	}
	
	/**
	 * getByUser
	 * 
	 *
	 * @return array
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function getByUser($uid) {
		$xpath = "//Entry[User = '". $uid ."']";
		return $this->xpath($xpath);
	}
	
	/**
	 * getById
	 * returns a TimeEntry by ID (position in list) and UserID
	 *
	 * @return array
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function getById($uid) {
		$xpath = "//Entry[UID = '". $uid ."']";
		$res = $this->xpath($xpath);
		return $res[0];
	}
	
	/**
	 * removeById
	 * removes a TimeEntry by ID (position in list) and UserID
	 *
	 * @return array
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function removeById($id, $uid) {
		$xpath = "//Entry[User = '". $uid ."']";
		$res = $this->xpath($xpath);
		unset($res);
		return true;
	}
	
	/**
	 * getWorkForTaskUID
	 * undocumented function
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public function sumWorkForTaskUID($tUID) {
		$sum = "";
		foreach ($this->getByTaskUID($tUID) as $entry) {
			$sum = ISODuration::add($sum, $entry->Work);
		}
		return $sum;
	}

}


?>